#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
struct segment_tree{
    int ma[N<<2];
    priority_queue<int> que[N<<2];
    void push_up(int now){
        ma[now] = max(ma[now<<1],ma[now<<1|1]);
        if(que[now].size()){
            ma[now] = max(ma[now],que[now].top());
        }
    }
    void build(int now,int l,int r){
        if(l == r){
            ma[now] = -1;
            return;
        }
        int mid = (l + r)/2;
        build(now<<1,l,mid);
        build(now<<1|1,mid+1,r);
        push_up(now);
    }
    void update(int now,int l,int r,int x,int y,int val){
        if(x <= l && r <= y){
            que[now].push(val);
            ma[now] = max(ma[now],val);
            return;
        }
        push_up(now);
        int mid = (l + r)/2;
        if(x <= mid){
            update(now<<1,l,mid,x,y,val);
        }
        if(mid < y){
            update(now<<1|1,mid+1,r,x,y,val);
        }
        push_up(now);
    }
    void rem(int now,int l,int r,int goal){
        if(ma[now] < goal){
            return;
        }
        if(que[now].size() && que[now].top() == goal){
            que[now].pop();
            if(l == r){
                if(que[now].size()){
                    ma[now] = que[now].top();
                }else{
                    ma[now] = -1;
                }
            }else{
                push_up(now);
            }
            return;
        }
        int mid = (l + r)/2;
        rem(now<<1,l,mid,goal);
        rem(now<<1|1,mid+1,r,goal);
        push_up(now);
    }
    void remove(int now,int l,int r,int x,int y,int goal){
        if(x <= l && r <= y){
            rem(now,l,r,goal);
            return;
        }
        int mid = (l + r)/2;
        if(que[now].size() && que[now].top() == goal){
            que[now<<1].push(que[now].top());
            ma[now<<1] = max(ma[now<<1],que[now].top());
            que[now<<1|1].push(que[now].top());
            ma[now<<1|1] = max(ma[now<<1|1],que[now].top());
            que[now].pop();
        }
        if(x <= mid){
            remove(now<<1,l,mid,x,y,goal);
        }
        if(mid < y){
            remove(now<<1|1,mid+1,r,x,y,goal);
        }
        push_up(now);
    }
    int query(int now,int l,int r,int x,int y){
        if(x <= l && r <= y){
            return ma[now];
        }
        int mid = (l + r)/2;
        int ma = -1;
        if(que[now].size()){
            ma = que[now].top();
        }
        if(x <= mid){
            ma = max(query(now<<1,l,mid,x,y),ma);
        }   
        if(mid < y){
            ma = max(query(now<<1|1,mid+1,r,x,y),ma);
        }
        return ma;
    }
} sg;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    sg.build(1,1,n);
    for(int i = 1;i <= m;i++){
        int op;
        cin>>op;
        if(op == 1){
            int l,r,k;
            cin>>l>>r>>k;
            sg.update(1,1,n,l,r,k);
        }else if(op == 2){
            int l,r;
            cin>>l>>r;
            int ma = sg.query(1,1,n,l,r);
            if(ma == -1)continue;
            sg.remove(1,1,n,l,r,ma);
        }else{
            int l,r;
            cin>>l>>r;
            int ma = sg.query(1,1,n,l,r);
            cout<<ma<<'\n';
        }
    }
}